/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DangNhap.java
 *
 * Created on 04-04-2012, 21:34:26
 */
package GUI;

import DAO.MySQLDataAccessHelper;
import POJO.NhanVienPoJo;
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author tran quoc thinh
 */
public class DangNhap extends javax.swing.JFrame {

    /** Creates new form DangNhap */
    public DangNhap() {
        initComponents();
    }

    public NhanVienPoJo nv = new NhanVienPoJo();    

//    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtxtUserName = new javax.swing.JTextField();
        jtxtPassword = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jbtnDangNhap = new javax.swing.JButton();
        jbtnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Đăng Nhập");

        jtxtUserName.setFont(new java.awt.Font("Tahoma", 1, 14));

        jtxtPassword.setFont(new java.awt.Font("Tahoma", 1, 14));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Username");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Password");

        jbtnDangNhap.setFont(new java.awt.Font("Tahoma", 1, 14));
        jbtnDangNhap.setText("Đăng Nhập");
        jbtnDangNhap.setName("jbtnDangNhap"); // NOI18N
        jbtnDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDangNhapActionPerformed(evt);
            }
        });
        jbtnDangNhap.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbtnDangNhapKeyPressed(evt);
            }
        });

        jbtnThoat.setFont(new java.awt.Font("Tahoma", 1, 14));
        jbtnThoat.setText("Thoát");
        jbtnThoat.setName("jbtnThoat"); // NOI18N
        jbtnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(jbtnDangNhap)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER, false)
                            .addComponent(jtxtUserName, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))))
                .addGap(63, 63, 63))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnDangNhap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(106, 106, 106))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbtnDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDangNhapActionPerformed
// TODO add your handling code here:
    try
    {
        boolean kqdangnhap = false;
        MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
        helper.Open();
        String sql=String.format("select * from NhanVien where UserName =BINARY '%s' and PassWord = BINARY '%s'", this.jtxtUserName.getText(),this.jtxtPassword.getText());
        ResultSet rs = helper.excecuteQuery(sql);
        while (rs.next())
        {
            nv.setDiaChi(rs.getString("DiaChi"));
            nv.setDienThoai(rs.getString("DienThoai"));
            nv.setMaNV(rs.getString("MaNV"));
            nv.setNgaySinh(rs.getDate("NgaySinh"));
            nv.setPassword(rs.getString("PassWord"));
            nv.setQuyenHan(rs.getInt("QuyenHan"));
            nv.setTenNV(rs.getString("TenNV"));
            nv.setUserName(rs.getString("UserName"));
            kqdangnhap = true;
        }
        if(kqdangnhap==true)
        {
            if(nv.getQuyenHan()==1)
            {
                this.dispose();
                Admin frmAdmin = new Admin(nv);
                frmAdmin.setExtendedState(JFrame.MAXIMIZED_BOTH);
                
                frmAdmin.setVisible(true);
            }
            else if(nv.getQuyenHan()==2)
            {
                this.dispose();
                NhanVien frmNhanVien = new NhanVien(nv);
                frmNhanVien.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frmNhanVien.setVisible(true);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Đăng Nhập Thất Bại! Kiểm Tra Lại UserName Và PassWord!");
        }
                
        helper.Close();
    }catch(Exception ex)
    {
        JOptionPane.showConfirmDialog(this, ex.getMessage());
    }
}//GEN-LAST:event_jbtnDangNhapActionPerformed

private void jbtnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThoatActionPerformed
// TODO add your handling code here:
    int kq= JOptionPane.showConfirmDialog(this, "Bạn có muốn thoát không","Thông báo",JOptionPane.YES_NO_OPTION);
    if(kq==JOptionPane.YES_OPTION)
    {
        this.dispose();
    }
}//GEN-LAST:event_jbtnThoatActionPerformed

private void jbtnDangNhapKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbtnDangNhapKeyPressed
// TODO add your handling code here:
    jbtnDangNhapActionPerformed(null);
}//GEN-LAST:event_jbtnDangNhapKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DangNhap().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jbtnDangNhap;
    private javax.swing.JButton jbtnThoat;
    private javax.swing.JPasswordField jtxtPassword;
    private javax.swing.JTextField jtxtUserName;
    // End of variables declaration//GEN-END:variables
}
